package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import util.Validate;
import bean.AllBean;
import bean.InsertUpdateDelBean;

public class YudingjiServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public YudingjiServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html;charset=gb2312");
		request.setCharacterEncoding("gb2312");
		
		String id = request.getParameter("id");
		String zuoxi = request.getParameter("zuoxi");
		String sums = request.getParameter("sums");
		String zhifu = request.getParameter("zhifu");
		HttpSession session = request.getSession();
		ArrayList userlogin = (ArrayList)session.getAttribute("userlogin");
		AllBean ab = new AllBean();
		ArrayList feiji = ab.getFeiji(id);
		Validate v = new Validate();
		String price = "";
		String sumsname = "";
		String zuo = "";
		if(zuoxi != null && zuoxi.equals("jingji")){
			price = feiji.get(9).toString();
			sumsname = "jingjisums";
			zuo = "经济舱";
		}
		if(zuoxi != null && zuoxi.equals("toudeng")){
			price = feiji.get(10).toString();
			sumsname = "toudengsums";
			zuo = "头等舱";
		}

		String sql = "insert into yudingji(usersid,feijiid,zuoxi,price,sums,zongjia,times,zhifu) values('"+userlogin.get(0)+"','"+id+"','"+zuo+"','"+price+"','"+sums+"','"+Integer.parseInt(price)*Integer.parseInt(sums)+"','"+v.getSystemDate().substring(0,10)+"','"+zhifu+"')";
		String str = "/users/yudingji_list.jsp";

		InsertUpdateDelBean ib = new InsertUpdateDelBean();
		
		ib.insertANDupdateANDdel(sql);
		sql = "update feiji set "+sumsname+"="+sumsname+"-"+sums+" where id="+id;
		ib.insertANDupdateANDdel(sql);
		request.setAttribute("message", "操作成功！");

		request.getRequestDispatcher(str).forward(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
